import request from '@/utils/request'

export function getList(type, params) {
  let url = ''
  switch (type) {
    case 'getFund':
      url = 'getbkFund'
      break
    case 'getFundList':
      url = 'getbkFundList'
      break
    case 'getRemain':
      url = 'getbkRemain'
      break
    case 'getConstant':
      url = 'getbkConstant'
      break
    case 'getConstantList':
      url = 'getbkConstantList'
      break
    default:
      break
  }
  return new Promise((resolve, reject) => {
    request({
      url,
      method: 'get',
      params
    })
      .then(res => {
        resolve(res)
      })
      // eslint-disable-next-line handle-callback-err
      .catch(error => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve(data)
      })
  })
}

export function addOne(type, data) {
  let url = ''
  switch (type) {
    case 'getFundAdd':
      url = 'getbkFundAdd'
      break
    case 'getConstantAdd':
      url = 'getbkConstantAdd'
      break
    case 'getRemainAdd':
      url = 'getbkRemainAdd'
      break
    case 'getRemainAddList':
      url = 'getbkRemainAddList'
      break
    default:
      break
  }
  return new Promise((resolve, reject) => {
    request({
      url,
      method: 'post',
      data
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve({ data })
      })
  })
}

export function updateOne(type, data) {
  let url = ''
  switch (type) {
    case 'getFundUpdate':
      url = 'getbkFundUpdate'
      break
    case 'getRemainUpdate':
      url = 'getbkRemainUpdate'
      break
    case 'getRemainBatchUpdate':
      url = 'getbkRemainBatchUpdate'
      break
    case 'getRemainCheck':
      url = 'getbkRemainCheck'
      break
    case 'getConstantUpdate':
      url = 'getbkConstantUpdate'
      break
    default:
      break
  }
  return new Promise((resolve, reject) => {
    request({
      url,
      method: 'post',
      data
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve({ data })
      })
  })
}

export function deleteOne(type, data) {
  let url = ''
  switch (type) {
    case 'getFundDelete':
      url = 'getbkFundDelete'
      break
    case 'getRemainDelete':
      url = 'getbkRemainDelete'
      break
    case 'getConstantDelete':
      url = 'getbkConstantDelete'
      break
    default:
      break
  }
  return new Promise((resolve, reject) => {
    request({
      url,
      method: 'post',
      data
    })
      .then(res => {
        resolve(res)
      })
      .catch(() => {
        const data = {
          total: 0,
          pageItems: []
        }
        resolve({ data })
      })
  })
}
